package com.example.yixueban.controller;


import com.example.yixueban.req.TestUserLoginReq;
import com.example.yixueban.req.TestUserRegiterReq;
import com.example.yixueban.req.main.Login_Req;
import com.example.yixueban.resp.CommonResp;
import com.example.yixueban.resp.TestUserLoginResp;
import com.example.yixueban.resp.login.AccountInfo_resp;
import com.example.yixueban.service.MainServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private MainServiceImpl mainService;

    @Value("${test.hello:TEST}")
    private String testHello;

    /**
     * GET, POST, PUT, DELETE
     * <p>
     * /user?id=1
     * /user/1
     *
     * @return
     */
    @GetMapping("/hello")
    public String hello() {
        return "Hello World" + testHello;
    }

    @PostMapping("/hello")
    public CommonResp hello1(@RequestBody TestUserRegiterReq regiterReq) {
        System.out.println(regiterReq.getUsername());
        CommonResp resp = new CommonResp();
        return resp;
    }

    @PostMapping("/login")
    public CommonResp login(@RequestBody TestUserLoginReq req) {
        CommonResp<TestUserLoginResp> resp = new CommonResp<>();
        TestUserLoginResp userLoginResp = new TestUserLoginResp();
        if (Integer.parseInt(req.getUserLogo()) == 1) {
            Long id = new Long(1);
            userLoginResp.setLoginName("大西");
            userLoginResp.setName("123456");
            userLoginResp.setId(id);
            userLoginResp.setToken("1");
        } else {
            Long id = new Long(2);
            userLoginResp.setLoginName("咕叽");
            userLoginResp.setName("654321");
            userLoginResp.setId(id);
            userLoginResp.setToken("2");
        }
        resp.setContent(userLoginResp);
        return resp;
    }

    @GetMapping("/logout/{id}")
    public CommonResp logout(@PathVariable long id) {
        System.out.println(id);
        CommonResp resp = new CommonResp();
        return resp;
    }

    @GetMapping(value = "/logout2")
    public CommonResp logout111() {
        System.out.println("layout2");
        CommonResp resp = new CommonResp();
        return resp;
    }

    @GetMapping(value = "kk/{depid}/{userid}")
    public CommonResp testw(@PathVariable("depid") String departmentID, @PathVariable("userid") String userid) {
        System.out.println(departmentID);
        System.out.println(userid);
        CommonResp resp = new CommonResp();
        return resp;
    }


    @GetMapping(value = "loginGet/{user_number}/{password}/{telephone}/{code}/{identity}")
    public CommonResp LoginGet(@PathVariable("user_number") String user_number, @PathVariable("password") String password, @PathVariable("telephone") String telephone, @PathVariable("code") String code, @PathVariable("identity") String identity) {
        CommonResp<AccountInfo_resp> resp = new CommonResp<>();
        System.out.println("user_number:" + user_number);
        System.out.println("password:" + password);
        System.out.println("telephone:" + telephone);
        System.out.println("code:" + code);
        System.out.println("identity:" + identity);
        Login_Req login_req = new Login_Req(user_number, password, telephone, code, identity);

        if (login_req.getPassword().equals("") && login_req.getTelephone().equals("")) {
            //输入了账号但是没写密码
            resp.setSuccess(false);
            resp.setMessage("请输入密码登录");
            return resp;
        }
        AccountInfo_resp accountInfo_resp = mainService.login(login_req);
        if (accountInfo_resp.getTemp() == 0) {
            resp.setContent(accountInfo_resp);
        } else {
            resp.setSuccess(false);
            if (accountInfo_resp.getTemp() == 1)
                resp.setMessage("登录失败，账号密码不匹配");
            else
                resp.setMessage("验证码错误");

        }
        return resp;
    }
}
